package pl.jtb.ctl.tabory.ui.se.handlers;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import pl.jtb.ctl.tabory.dao.data.sl.UserRoleSL;
import pl.jtb.ctl.tabory.ui.editors.OpenViewEditorAction;
import pl.jtb.ctl.tabory.ui.osgi.UiBundleActivator;
import pl.jtb.ctl.tabory.ui.se.ui.SecurityEditor;
import pl.jtb.ctl.tabory.ui.security.CredentialsValidaton;

public class OpenSecurityViewHandler extends AbstractHandler {

    public OpenSecurityViewHandler() {
    }

    public Object execute(ExecutionEvent event) throws ExecutionException {
        if (CredentialsValidaton.getInstance().isValidFor(UserRoleSL.ADMINISTRATOR)) {
            IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
            OpenViewEditorAction action = new OpenViewEditorAction(window, "label", SecurityEditor.ID);
            action.run();
        } else {
            MessageDialog.openError(Display.getDefault().getActiveShell(), UiBundleActivator.getMessage("credentials.error.title"), UiBundleActivator.getMessage("credentials.error.message"));
        }
        return null;
    }
}
